package learn.Thread;

import java.util.concurrent.Semaphore;

public class LearnSemaphore {
    public static void main(String[] args) {
        //实现runnable接口来创建线程
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 10; i++) {
            Runnable r1 = new writeThread(semaphore);
            Thread t1 = new Thread(r1);
            t1.start();
        }


    }
}

class writeThread implements Runnable{
    private Semaphore semaphore;

    public writeThread(Semaphore semaphore){
        this.semaphore = semaphore;
    }
    @Override
    public void run() {
        try {
            semaphore.acquire();
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前执行线程："+Thread.currentThread().getName());
        semaphore.release();

    }
}